import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../util.dart';

showActionSheet({
  required List<String> titles,
  List<int>? choosedIndexs,
  String? title,
  bool isMutiableChoose = false,
  bool canBack = false,
  VoidCallback? backOnTap,
  Function(List<int>)? onTap,
}) {
  if (Get.context == null) {
    return;
  }

  var _query = MediaQuery.of(Get.context!);
  showModalBottomSheet(
      context: Get.context!,
      constraints: BoxConstraints.expand(
          height: titles.length * 50.r + _query.padding.bottom + 100.r),
      builder: (context) {
        return BottomSheetView(
          titles: titles,
          title: title,
          choosedIndexs: choosedIndexs,
          isMutiableChoose: isMutiableChoose,
          backOnTap: backOnTap,
          onTap: onTap,
        );
      });
}

class BottomSheetController extends GetxController {
  // List<int>? choosedIndexs;
}

class BottomSheetView extends StatelessWidget {
  BottomSheetView({
    Key? key,
    required this.titles,
    this.title,
    this.choosedIndexs,
    this.isMutiableChoose = false,
    this.backOnTap,
    this.onTap,
  }) : super(key: key);
  final List<String> titles;
  List<int>? choosedIndexs;
  String? title;
  bool isMutiableChoose = false;
  VoidCallback? backOnTap;
  Function(List<int>)? onTap;

  final controller = Get.put(BottomSheetController());

  @override
  Widget build(BuildContext context) {
    // controller.choosedIndexs = choosedIndexs;
    // var _choosedIndexs = controller.choosedIndexs;
    return GetBuilder<BottomSheetController>(builder: (controller) {
      return ClipRRect(
        borderRadius: BorderRadius.circular(10.r),
        child: Column(
          children: [
            Container(
              height: 50.r,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: gap12),
              decoration: const BoxDecoration(
                  border: Border(
                      bottom: BorderSide(color: Color(0xffeaeaea), width: 1))),
              child: Stack(
                children: [
                  Center(
                    child: Text(
                      title ?? '请选择',
                      style:
                          pfSCStyle(color: ColorsUtil.title, fontSize: 16.sp),
                    ),
                  ),
                  Positioned(
                      right: gap12,
                      child: isMutiableChoose
                          ? GestureDetector(
                              onTap: () {
                                if (onTap != null) {
                                  onTap!(choosedIndexs ?? []);
                                }
                                Get.back();
                              },
                              child: Container(
                                height: 50.r,
                                alignment: Alignment.center,
                                child: Text(
                                  '确定',
                                  style: pfSCStyle(
                                      color: ColorsUtil.blue, fontSize: 16.sp),
                                ),
                              ),
                            )
                          : SizedBox()),
                  Positioned(
                      left: gap12,
                      child: backOnTap != null
                          ? GestureDetector(
                              onTap: () {
                                Get.back();
                                Future.delayed(Duration(milliseconds: 300))
                                    .then((value) {
                                  backOnTap!();
                                });
                              },
                              child: Container(
                                height: 50.r,
                                alignment: Alignment.centerLeft,
                                child: Icon(
                                  Icons.arrow_back_ios,
                                  color: ColorsUtil.content,
                                ),
                              ),
                            )
                          : SizedBox())
                ],
              ),
            ),
            Expanded(
              child: ListView(
                children: titles
                    .asMap()
                    .map(
                      (key, value) => MapEntry(
                        key,
                        GestureDetector(
                          onTap: () {
                            if (isMutiableChoose) {
                              if (choosedIndexs?.contains(key) == true) {
                                choosedIndexs!.remove(key);
                              } else {
                                if (choosedIndexs == null) {
                                  choosedIndexs = [key];
                                } else {
                                  choosedIndexs!.add(key);
                                }
                              }

                              controller.update();
                            } else {
                              Navigator.of(context).pop();
                              if (onTap != null) {
                                onTap!([key]);
                              }
                            }
                          },
                          behavior: HitTestBehavior.translucent,
                          child: Container(
                            height: 50.r,
                            padding: EdgeInsets.symmetric(horizontal: gap12),
                            decoration: const BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(
                                        color: Color(0xffeeeeee), width: 1))),
                            child: Row(
                              mainAxisAlignment: isMutiableChoose
                                  ? MainAxisAlignment.spaceBetween
                                  : MainAxisAlignment.center,
                              children: [
                                Container(
                                  alignment: Alignment.center,
                                  child: Text(
                                    value,
                                    style: TextStyle(
                                        color:
                                            choosedIndexs?.contains(key) == true
                                                ? ColorsUtil.green
                                                : const Color(0xff333333),
                                        fontSize: 14.sp),
                                  ),
                                ),
                                isMutiableChoose &&
                                        choosedIndexs?.contains(key) == true
                                    ? Icon(
                                        Icons.check,
                                        color: ColorsUtil.green,
                                      )
                                    : SizedBox(),
                              ],
                            ),
                          ),
                        ),
                      ),
                    )
                    .values
                    .toList(),
              ),
            ),
            SafeArea(
              child: GestureDetector(
                onTap: Get.back,
                child: Container(
                  height: 50.r,
                  alignment: Alignment.center,
                  decoration: const BoxDecoration(
                    border: Border(
                      bottom: BorderSide(color: Color(0xffeaeaea), width: 1),
                      top: BorderSide(color: Color(0xffeaeaea), width: 1),
                    ),
                  ),
                  child: Text(
                    '取消',
                    style: pfSCStyle(color: ColorsUtil.title, fontSize: 16.sp),
                  ),
                ),
              ),
            )
          ],
        ),
      );
    });
  }
}
